/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#include "gd25q_cmd.h"
#include "gd25q_hal.h"
#include "gd25q_manage.h"
#include "os_obj.h"
#include "common.h"

q25_req_type * q25_req;
rt_ubase_t mb_rcv;

uint32_t q25_test_buff[512];

/*----------------------------------------------------------------------------
 *                             q25 task
 *---------------------------------------------------------------------------*/
void q25_task(void   *argument)
{
    rt_err_t rt_st;
    
    osDelay(10);
    gdSpiInit();
    gd25_init(0);
    
    gd25_id_read((uint8_t*)q25_test_buff);
    GD25_DBG_INFO("gd25 id : 0x%08x \n", q25_test_buff[0]);
    
    gd25_read(0,1024,(uint8_t*)q25_test_buff);
    GD25_DBG_INFO("gd25 read : 0x%08x, 0x%08x, 0x%08x, 0x%08x \n", q25_test_buff[0],q25_test_buff[1],q25_test_buff[2],q25_test_buff[3]);
    GD25_DBG_INFO("gd25 read : 0x%08x, 0x%08x, 0x%08x, 0x%08x \n", q25_test_buff[4],q25_test_buff[5],q25_test_buff[6],q25_test_buff[7]);
    GD25_DBG_INFO("gd25 read : 0x%08x, 0x%08x, 0x%08x, 0x%08x \n", q25_test_buff[8],q25_test_buff[9],q25_test_buff[10],q25_test_buff[11]);
    GD25_DBG_INFO("gd25 read : 0x%08x, 0x%08x, 0x%08x, 0x%08x \n", q25_test_buff[12],q25_test_buff[13],q25_test_buff[14],q25_test_buff[15]);
    
    for(;;)
    {
        mb_rcv = 0;
        rt_st = rt_mb_recv(os_obj.qbid_flash, &mb_rcv, INT16_MAX);
        
        if((RT_EOK == rt_st) && ( 0 != mb_rcv))
        {
            q25_req = (q25_req_type * )mb_rcv;
            q25_req->St = Q25_ST_BUSY;
            switch(q25_req->rw_mode)
            {
                case Q25_MODE_READ:
                    gd25_read(q25_req->Addr, q25_req->Len, (uint8_t*)q25_req->data);
                    break;
                case Q25_MODE_WRITE:
                    gd25_write(q25_req->Addr, q25_req->Len, q25_req->data);
                    break;
                case Q25_MODE_Sector_Erase:
                    gd25_sector_erase(q25_req->Addr);
                    break;
                case Q25_MODE_Block_Erase_32K:
                    gd25_block_erase(q25_req->Addr);
                    break;
                case Q25_MODE_Chip_Erase:
                    gd25_chip_erase();
                    break;
                default:
                    break;
            }
            
            if(q25_req->CallBack)
                q25_req->CallBack();
            
            q25_req->St = Q25_ST_COMP;
        }
    }
}

/*----------------------------------------------------------------------------
 *                             q25 req
 *---------------------------------------------------------------------------*/
void q25_cmd_req(uint32_t q25_par_addr)
{
    rt_mb_send(os_obj.qbid_flash, q25_par_addr);
}
